package com.example.microinvestmarket.service.impl;
import com.example.microinvestmarket.service.UserTradeImitateService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Random;

@Service
public class UserTradeImitateServiceImpl implements UserTradeImitateService {


    private final Random random = new Random();

    //模拟购买花费时间
    @Override
    public long UserBuyTimeImitate(BigDecimal buyPrice, BigDecimal nowPrice) {
        // 计算价格差距
        double judgeTime = buyPrice.subtract(nowPrice).doubleValue();
        long ttl;

        if (judgeTime == 0) {
            // 价格相同，随机时间较短
            ttl = random.nextInt(1000, 10000);
        } else if (judgeTime < -10) {
            // 价格比买入价低很多，极难卖出，时间极长
            ttl = 100000000L;
        } else if (judgeTime < -5) {
            // 价格比买入价低较多，难卖出，时间较长
            ttl = random.nextInt(30000000, 90000000);
        } else if (judgeTime < 0) {
            // 价格略低于买入价，较难卖出
            ttl = random.nextInt(2000000, 8000000);
        } else if (judgeTime < 5) {
            // 价格略高于买入价，容易卖出
            ttl = random.nextInt(500000, 1500000);
        } else {
            // 价格明显高于买入价，极易卖出，时间最短
            ttl = random.nextInt(1000, 5000);
        }

        return ttl;

    }

    //模拟卖出花费时间
    @Override
    public long UserSellTimeImitate(BigDecimal sellPrice, BigDecimal nowPrice) {
        double judgeTime = sellPrice.subtract(nowPrice).doubleValue();
        long ttl;

        if (judgeTime == 0) {
            ttl = random.nextInt(1000, 10000);
        } else if (judgeTime > 10) {
            ttl = 100000000L; // 极难卖出
        } else if (judgeTime > 5) {
            ttl = random.nextInt(30000000, 90000000);
        } else if (judgeTime > 0) {
            ttl = random.nextInt(2000000, 8000000);
        } else if (judgeTime > -5) {
            ttl = random.nextInt(500000, 1500000);
        } else {
            ttl = random.nextInt(1000, 5000); // 价格低，卖出快
        }

        return ttl;
    }

}
